package com.neuedu.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.neuedu.dto.PageData;
import com.neuedu.po.Emp;
import com.neuedu.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class EmpController {

    @Autowired
    EmpService service;

    @RequestMapping("/getEmpList")
    public Map<String,Object> getEmpList(@RequestBody PageData pageData) {
        Page<Object> startPage = PageHelper.startPage(pageData.getCurrentPage(), pageData.getPageSize()); // 分页（当前页面，每页数据条数）
        Map<String,Object> map = new HashMap<>();

        List<Emp>  empList = new ArrayList<>(); // 当前页数据

        if(pageData.getSearch() == null || pageData.getSearch().equals("")) {
            empList = service.getEmpList();
        }else{
            empList = service.getEmpListByEname(pageData.getSearch());
        }
        long total = startPage.getTotal(); // 获取数据总条数

        map.put("empList",empList);
        map.put("total",total);
        return map;
    }
    @RequestMapping("/editEmp")
    public int editEmp(@RequestBody Emp emp) {
        return service.editEmp(emp);
    }

    @RequestMapping("/deleteEmp")
    public int deleteEmp(Integer empno) {
        return service.deleteEmp(empno);
    }
    @RequestMapping("/insertEmp")
    public int insertEmp(@RequestBody Emp emp) {
        return service.insertEmp(emp);
    }
}
